---
title: Persist and remove a document
summary: Writes a document to disk, or removes its backing file.
status: draft
owner: SAIF Doc Gen
last_reviewed: 2026-09-17
library: PdfAPI.Core
reference_type: package
operation_id: OP-10
trigger: Document.Save(string) / Document.Delete() / Document.Dispose() invoked
---

# Persist and remove a document

This operation covers `Document`'s file-system write and delete lifecycle, including the destructive cascade from `Dispose()`.

| Field | Value |
| --- | --- |
| **Operation ID** | OP-10 |
| **Trigger** | Caller invokes `Document.Save(string destinationDirectory)`, `Document.Delete()`, or `Document.Dispose()` |
| **Inputs** | Destination directory (`Save`), or none (`Delete`/`Dispose`) |
| **Outputs** | File written to disk, or file removed; `true`/`false` from `Delete` |
| **Primary components** | `PdfAPI.Core.Document.Save`, `Document.Delete`, `Document.Dispose` |
| **Entry point** | `PdfAPI.Core\Document.cs` |

## Steps

1. `Save`: set `FilePath` to `{destinationDirectory}\{FileName}` and write all bytes to that path. ([BR-22](../business-rules.md#br-22-saving-a-document-writes-to-destination-directory))
   - `Save` composes the path with a hard-coded `\` separator rather than `Path.Combine`.*
2. `Delete`: delete `FilePath` when it exists and return `true`; swallow any exception and return `false`. ([BR-23](../business-rules.md#br-23-document-deletion-never-throws))
3. `Dispose`: call `Delete()`, so disposing a document removes its file from disk. ([BR-24](../business-rules.md#br-24-disposing-a-document-deletes-its-file))

## Process flow

The following diagram visualizes this operation's control flow.

```mermaid
flowchart TD
	SaveStart(["Save(destinationDirectory)"]) --> SetPath["FilePath = dir\FileName"]
	SetPath --> Write["Write all bytes"]
	Write --> SaveDone([File persisted])

	DeleteStart(["Delete()"]) --> Exists{FilePath exists?}
	Exists -->|Yes| Del["File.Delete"]
	Del --> True([Return true])
	Exists -->|No| False1([Return false])

	DisposeStart(["Dispose()"]) --> CallDelete["Call Delete()"]
	CallDelete --> DisposeDone([File removed])
```

## Failure and edge-case behavior
- Any exception during deletion (e.g., file locked, permission denied) is swallowed and reported as `false` with no diagnostic ([BR-23](../business-rules.md#br-23-document-deletion-never-throws)).
- Disposing a `Document` obtained from a caller-owned source file destroys that file, since `Dispose()` unconditionally deletes it ([BR-24](../business-rules.md#br-24-disposing-a-document-deletes-its-file)).

> **Note:** Statements marked with `*` were inferred from code structure, naming
> conventions, or contextual evidence where direct confirmation was not available
> in source artifacts.

## Related documentation
- [Operations and processes](../operations-processes.md)
- [Business rules](../business-rules.md)
- [Security and authorization](../security-authorization.md)

## Sources
- `PdfAPI.Core\Document.cs`
